package com.leezk.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leezk.order.dto.SystemMsg;
import com.leezk.order.entity.Admin;
import com.leezk.order.entity.Orders;
import com.leezk.order.exception.NoDataException;
import com.leezk.order.exception.OperateException;
import com.leezk.order.exception.ParamsException;
import com.leezk.order.mapper.AdminMapper;
import com.leezk.order.mapper.MenuMapper;
import com.leezk.order.mapper.OrdersMapper;
import com.leezk.order.mapper.StudentMapper;
import com.leezk.order.service.IAdminService;
import com.leezk.order.utils.StringUtil;
import freemarker.template.utility.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author leotong
 * @since 2022-03-16
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Override
    public String login(Admin admin)  {
        if (admin==null||admin.getAccount()==null||admin.getPassword()==null){
            throw new ParamsException("登陆参数不能为空，请检查后再次登陆");
        }
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",admin.getAccount());
        Admin adminResult = this.getOne(queryWrapper);
        if (adminResult==null){
            throw new NoDataException("当前用户不存在");
        }
        if (!adminResult.getPassword().equals(admin.getPassword())){
            throw new OperateException("密码输入错误");
        }
        String createToken = StringUtil.createToken();
        return createToken;
    }
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public SystemMsg getSystemMsg() {
        SystemMsg systemMsg=new SystemMsg();
        systemMsg.setStuNum(Math.toIntExact(studentMapper.selectCount(null)));
        systemMsg.setMenuNum(Math.toIntExact(menuMapper.selectCount(null)));
        SimpleDateFormat wrapperSdf1 = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        //当前时间
        Date dzDateObj = new Date();
        String nowTime = wrapperSdf1.format(dzDateObj);
        //当天0.00点
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("creatTime",zero,new Date());
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        systemMsg.setTodayOrderNum(orders.size());
        for (Orders e : orders) {
            BigDecimal price = systemMsg.getPrice();
            systemMsg.setPrice(e.getPrice().add(price==null?new BigDecimal(0):price));
        }
        return systemMsg;
    }
}
